<template>
  <div class="datacenter">
    <div class="datacenter_con">
      <div class="tit">
        <div class="line"></div>
        <div class="text">消息队列</div>
      </div>
      <div class="btn_row">
        <el-button type="primary" size="small" @click="addFun"
          >添加消息队列</el-button
        >
        <div class="search_box">
          <el-input v-model="content" placeholder="请输入名称"></el-input>
          <el-button
            type="primary"
            @click="searchFun()"
            size="small"
            icon="el-icon-search"
          ></el-button>
        </div>
      </div>
      <table-view
        :tData="tData"
        :tHead="tHead"
        @deleteFun="deleteFun"
        @editMsg="editMsg"
      ></table-view>
      <pagination
        class="datacenter_con_fenye"
        :pageSize="pageSize"
        :total="total"
        @paginationCallBack="paginationCallBack"
      ></pagination>
    </div>
    <transition name="fade">
      <div class="detail" v-if="isAlert">
        <div class="detail_box detail_box_form">
          <div class="top">
            <div class="edit_text">{{ editName }}</div>
            <div class="close" @click="cancel()">X</div>
          </div>
          <div class="top">
            <div class="edit_text edit_text_left">队列信息</div>
          </div>
          <div class="from_box">
            <el-form
              class="form_item"
              :rules="rules"
              ref="form1"
              :model="form1"
              label-width="110px"
            >
              <el-form-item label="队列类型" prop="queueNamee">
                <!-- <el-input v-model="form.topic"></el-input> -->
                <el-select
                  v-model="form1.type"
                  @change="selects"
                  :disabled="disab"
                >
                  <el-option
                    v-for="(item, idx) in optionss"
                    :key="idx"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="地址" prop="namesrvAddr">
                <el-input
                  v-model="form1.namesrvAddr"
                  :disabled="disab"
                ></el-input>
              </el-form-item>
              <el-form-item label="accessKey" v-if="isshow">
                <el-input
                  v-model="form1.accessKey"
                  :disabled="disab"
                ></el-input>
              </el-form-item>
              <el-form-item label="secretKey" v-if="isshow">
                <el-input
                  v-model="form1.secretKey"
                  :disabled="disab"
                ></el-input>
              </el-form-item>
              <el-form-item label="topic" prop="topic">
                <el-input v-model="form1.topic" :disabled="disab"></el-input>
              </el-form-item>
              <el-form-item label="groupId" prop="groupId">
                <el-input v-model="form1.groupId" :disabled="disab"></el-input>
              </el-form-item>
              <el-form-item label="tag" v-if="isshow">
                <el-input v-model="form1.tag" :disabled="disab"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div id="top_margin">
            <div class="top"></div>
            <div class="top">
              <div class="edit_text edit_text_left">基本信息</div>
            </div>
            <div class="from_box">
              <el-form
                class="form_item"
                :rules="rules"
                ref="form"
                :model="form"
                label-width="110px"
              >
                <!-- <el-form-item label="队列名称" prop="topic">
                                <el-input :disabled="istopic" v-model="form.topic"></el-input>
                            </el-form-item> -->
                <el-form-item label="资产名称" prop="name">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="目标域" prop="region">
                  <el-input v-model="form.region"></el-input>
                </el-form-item>

                <!-- <el-form-item label="topic" prop="topic">
                                <el-input type="text" v-model="form.topic"></el-input>
                            </el-form-item> -->

                <!-- <el-form-item label="队列地址" prop="ip">
                                <el-input v-model="form.ip"></el-input>
                            </el-form-item> -->
                <el-form-item label="资产描述">
                  <el-input v-model="form.describes"></el-input>
                </el-form-item>
                <el-form-item label="发布者姓名" prop="issueName">
                  <el-input v-model="form.issueName"></el-input>
                </el-form-item>
                <el-form-item label="发布者邮箱">
                  <el-input v-model="form.issueMail"></el-input>
                </el-form-item>
                <el-form-item label="发布者机构" prop="issueOrganization">
                  <!-- <el-input v-model="form.topic"></el-input> -->
                  <el-select
                    v-model="form.issueOrganization"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, idx) in options"
                      :key="idx"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="发布者电话">
                  <el-input v-model="form.issueIphone"></el-input>
                </el-form-item>
                <el-form-item
                  id="yylb"
                  label="业务类别"
                  ref="yw_lb_name"
                  prop="yw_lb_name"
                >
                  <!-- <el-input
                    v-model="yw_lb_name"
                    style="display: none"
                  ></el-input> -->
                
                    <el-cascader
                      ref="cascade"
                      :class="{'cascade':iscolor}"
                      v-model="yw_lb_name"
                      :options="optionz"
                      :props="{ expandTrigger: 'hover' }"
                      @change="handleSelect"
                      :placeholder="ywlb"
                    ></el-cascader>
                </el-form-item>
                <!-- <el-form-item label="创建时间" props="createTime">
                                <div class="timeBox">
                                    <el-date-picker v-model="form.createTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
                                     </el-date-picker>
                                </div>
                            </el-form-item> -->
              </el-form>
              <!-- <div class="table_label">属性</div> -->
              <div class="table_content">
                <!-- <div class="icon_box" @click="addTableFun"><i class="el-icon-circle-plus-outline"></i></div>
                            <div class="table_tab">
                                <add-table :tableData="tableData" :table_Head="table_Head" :options_tab="options_tab" @del_tab_fun="del_tab_fun"></add-table>
                            </div> -->
                <!-- <el-table
                                :data="tableData"
                                border
                                :header-cell-style="biaotou_yangshi"
                                style="width: 100%">
                                <el-table-column
                                v-for="(v, index) in table_Head"
                                :key="index"
                                :prop="v.field"
                                :label="v.title"
                                :min-width="130"
                                :width="v.width"
                                ref="thDom">
                                <template slot-scope="scope">
                                    <el-input v-if="index != 2" v-model="scope.row[v.field]" />
                                    <el-select v-else v-model="scope.row.c" placeholder="请选择">
                                        <el-option
                                        v-for="item in options_tab"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                        </el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="100">
                                <template slot-scope="scope">
                                    <div class="delete_box" @click="del_tab_fun(scope.row, scope.$index)"><i class="el-icon-delete"></i></div>
                                </template>

                            </el-table-column>
                            </el-table> -->
              </div>
              <div class="btn_box">
                <el-button size="small" @click="cancel()">取消</el-button>
                <el-button size="small" type="primary" @click="saveOrUpdate"
                  >保存</el-button
                >
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <transition name="fade">
      <div class="detail" v-if="isDelAlert">
        <div class="detail_box">
          <div class="top">
            <div class="edit_text">删除</div>
            <div class="close" @click="cancelDel()">X</div>
          </div>
          <div class="from_box from_box_text">
            <div>确认删除{{ dele_msg.content }}！</div>
          </div>
          <div class="btn_group">
            <el-button size="small" @click="cancelDel">取消</el-button>
            <el-button type="primary" size="small" @click="ensureFun"
              >确定</el-button
            >
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import TableView from "../common/tableView";
import pagination from "../common/pagination";
import {
  listApi,
  saveOrUpdate,
  deleteApi,
  getall_options,
  getall_optionss,
} from "@/api/testRequest";
import { throttle } from "@/utils/utils";
import AddTable from "../common/addTable";
export default {
  name: "datacenter",
  components: {
    TableView,
    pagination,
    AddTable,
  },
  data() {
    var names = (rule, value, callback) => {
      var nameReg = /^(?:[\u4e00-\u9fa5·]{2,16})$/;
      if (!value) {
        return callback(new Error("请输入名称"));
      } else if (!nameReg.test(value)) {
        return callback(new Error("请输入正确的名称"));
      } else {
        callback();
      }
    };
    var ips = (rule, value, callback) => {
      var nameReg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
      if (!value) {
        return callback(new Error("请输入队列地址"));
      } else if (!nameReg.test(value)) {
        return callback(new Error("请输入正确的队列地址"));
      } else {
        callback();
      }
    };

    var issueName = (rule, value, callback) => {
      var nameReg = /^(?:[\u4e00-\u9fa5·]{2,16})$/;
      if (!value) {
        return callback(new Error("请输入队列地址"));
      } else if (!nameReg.test(value)) {
        return callback(new Error("请输入正确的队列地址"));
      } else {
        callback();
      }
    };
    //手机号
    var telephoneValid = (rule, value, callback) => {
      var teleReg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
      if (!value) {
        return callback(new Error("请输入手机号"));
      } else if (!teleReg.test(value)) {
        return callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    // yw_lb_names
    var yw_lb_names = (rule, value, callback) => {
      if (!this.yw_lb_name) {
        return callback(new Error("请输选择业务类别"));
      } else {
        callback();
      }
    };
    return {
      editName: "",
      istopic: false,
      rules: {
        name: [
          //姓名
          { required: true, message: "请输入资产名称", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        ip: [{ required: true, validator: ips, trigger: "blur" }],
        issueName: [{ required: true, message: "请输入内容", trigger: "blur" }],
        issueOrganization: [
          { required: true, message: "请选择发布机构", trigger: "change" },
        ],
        issueIphone: [
          { required: true, validator: telephoneValid, trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "请选择发布时间", trigger: "change" },
        ],
        yw_lb_name: [
          { required: true, validator: yw_lb_names, trigger: "click" },
        ],
        region: [{ required: true, message: "请输入目标域", trigger: "blur" }],
        namesrvAddr: [
          { required: true, message: "请输入地址", trigger: "blur" },
        ],
        dresss: [{ required: true, message: "请输入tag", trigger: "blur" }],
        queueName: [
          { required: true, message: "请输入队列名称", trigger: "blur" },
        ],
        queueNamee: [
          { required: true, message: "请输入队列类型", trigger: "blur" },
        ],
        topic: [{ required: true, message: "请输入topic", trigger: "blur" }],
        groupId: [
          { required: true, message: "请输入groupId", trigger: "blur" },
        ],
      },
      content: "", // 搜索input
      tHead: [
        // 表头
        { field: "orderNum", title: "序号",width: 60 },
        { field: "name", title: "资产名称" ,width: 216},
        { field: "type", title: "队列类型",width: 150 },
        { field: "topic", title: "topic" ,width: 150},
        { field: "namesrvAddr", title: "地址",width: 150 },
        { field: "groupId", title: "groupId" ,width: 150},
        // { field: "content", title: "内容" },
        { field: "createTime", title: "创建时间"},

        // { field: "", title: "消息队列名称" },
        // { field: "ip", title: "队列地址" },
        // { field: "topic", title: "topic" },
        // { field: "describes", title: "资产描述" },
        // { field: "issueName", title: "发布者姓名" },
        // // { field: "issueMail", title: "发布者邮箱" },
        // { field: "issueOrganization", title: "发布者机构" },
        // { field: "issueIphone", title: "发布者电话" },
        // { field: "business", title: "业务类别" },
      ],
      tData: [], // 表内容
      form: {
        // 添加弹窗中相关input
        name: "",
        region: "",
        // topic: '',
        describes: "",
        issueName: "",
        issueMail: "",
        issueOrganization: "",
        issueIphone: "",
        business: "",
        // createTime: this.getNowTime(),
      },
      form1: {
        type: "rocketmq",
        namesrvAddr: "",
        accessKey: "",
        secretKey: "",
        topic: "",
        groupId: "",
        tag: "",
      },
      isAlert: false, // 添加弹窗
      pageNo: 1,
      pageSize: 10, //每页显示几条
      total: 10, //总条数
      isDelAlert: false, // 删除弹窗
      // options: [],
      options: [],
      value: "",
      optionz: [],
      optionss: [
        {
          value: 1,
          name: "rocketmq",
        },
        {
          value: 2,
          name: "kafka",
        },
      ],
      dele_msg: "", // 删除当前行的名称
      yw_lb_name: "请选择", //业务类别
      table_Head: [
        { field: "a", title: "序号" },
        { field: "b", title: "字段名称" },
        { field: "c", title: "字段类型" },
        { field: "d", title: "字段长度" },
        { field: "e", title: "字段描述" },
      ],
      tableData: [],
      options_tab: [
        {
          value: "INT",
          label: "INT",
        },
        {
          value: "VARCHAR",
          label: "VARCHAR",
        },
        {
          value: "DATE",
          label: "DATE",
        },
      ],
      isDisabled: false,
      placehol: 2,
      isshow: true,
      type: "",
      disab: false,
      dary:'',
      ywlb:'请选择',
      yw_lb_name:'123',
      iscolor:false
    };
    // rocketMQ
  },
  mounted() {
    this.listApi();
    this.getall_options();
    this.getall_optionss();
  },
  methods: {
    getall_options() {
      getall_options().then((res) => {
        this.options = JSON.parse(res.data.dicValue);
      });
    },
    getall_optionss() {
      getall_optionss().then((res) => {
        this.optionz = JSON.parse(res.data.dicValue);
      });
    },
    addTableFun() {
      var obj = {
        a: "",
        b: "",
        c: "",
        d: "",
        e: "",
      };
      this.tableData.push(obj);
    },
    del_tab_fun(msg, idx) {
      this.tableData.splice(idx, 1);
    },
    getNowTime() {
      var now = new Date();
      var year = now.getFullYear(); //得到年份
      var month = now.getMonth(); //得到月份
      var date = now.getDate(); //得到日期
      var hour = " 00:00:00"; //默认时分秒 如果传给后台的格式为年月日时分秒，就需要加这个，如若不需要，此行可忽略
      month = month + 1;
      month = month.toString().padStart(2, "0");
      date = date.toString().padStart(2, "0");
      var defaultDate = `${year}-${month}-${date}`;
      return defaultDate;
    },
    // 页面加载获取查询列表
    listApi() {
      listApi({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        name: this.content,
      }).then((res) => {
        var i = 1;
        for (var j in res.data.rows) {
          res.data.rows[j].orderNum = i++;
          if (res.data.rows[j].type == 1) {
            res.data.rows[j].type = "rocketmq";
          } else {
            res.data.rows[j].type = "kafka";
          }
        }
        // this.options = res.data.organization

        this.tData = res.data.rows;
        this.total = res.data.total;
      });
    },
    // 保存
    saveOrUpdate() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form1.type == "rocketmq") {
            this.form1.type = 1;
          } else {
            this.form1.type = 2;
          }
          saveOrUpdate({
            ...this.form,
            ...this.form1,
            attrs: JSON.stringify(this.tableData),
            business: this.dary,
          }).then((res) => {
            if (res.code == 0) {
              if (this.istopic) {
                this.$message({
                  message: "编辑成功",
                  type: "success",
                });
              } else {
                this.form1 = {};
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
              }
              this.cancel();
              this.listApi();
            
            }
          });
        } else {
          return false;
        }
      });
    },
    liebiao_yangshi() {
      return "text-align:center;color:#333333;font-size:14px;";
    },
    biaotou_yangshi() {
      return "text-align:center;background:#f1f6ff;color:#333333;font-size:14px";
    },
    // 删除
    deleteFun(msg) {
      this.isDelAlert = true;
      this.dele_msg = msg;
    },
    ensureFun() {
      deleteApi({
        id: this.dele_msg.id,
      }).then((res) => {
        if (res.code == 0) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.listApi();
          this.isDelAlert = false;
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
          });
        }
      });
    },
    //删除弹窗  确认按钮
    // ensureFun () {
    //     this.$message({
    //         message: '恭喜你，这是一条成功消息',
    //         type: 'success'
    //     });
    // },

    selects(val) {
      // this.form1.type = val

      if (val == 2) {
        this.isshow = false;
      } else {
        this.isshow = true;
      }
    },
    // 添加按钮
    addFun() {
      this.istopic = false;
      this.isAlert = true;
      this.disab = false;
      this.editName = "添加";
      this.ywlb = '请选择';
      this.form1.type = 'rocketmq'
      this.iscolor = false;
    },

    // 新增~编辑中的关闭取消按钮
    cancel() {
      Object.keys(this.form).forEach((key) => {
        this.form[key] = "";
      });
      this.yw_lb_name = "";
      this.form1 = {};
      this.tableData = [];
      this.isAlert = false;
      this.isDisabled = false;
    },
    // 分页点击事件
    paginationCallBack(pageNo) {
      this.pageNo = pageNo;
      this.listApi();
    },
    // 关闭删除弹窗
    cancelDel() {
      this.isDelAlert = false;
    },

    handleSelect(key, keyPath) {
    //   this.yw_lb_name = key;
    //   this.$refs.yw_lb_name.clearValidate();
        if(key.length == 1){
            this.dary = key[0]
        }else{
            let str = '';
       for(let i=0;i<key.length;i++){
           str += key[i]+'/'        
      }
        this.dary = str.substr(0,str.length-1)
        }
    },
    // 搜索按钮
    searchFun: throttle(function () {
      this.pageNo = 1;
      this.listApi();
    }, 2000),

    // 编辑回调
    editMsg(msg) {
        this.listApi();
      this.ywlb = msg.business
      this.istopic = true;
      this.disab = true;
      this.iscolor = true;
      this.editName = "编辑";
      this.isDisabled = true;
      if (msg.type !== "rocketmq") {
        this.isshow = false;
      } else {
        this.isshow = true;
      }
      this.form1.type = msg.type;
      // this.yw_lb_name = msg.business;

      this.form1.namesrvAddr = msg.namesrvAddr;
      this.form1.accessKey = msg.accessKey;
      this.form1.secretKey = msg.secretKey;
      this.form1.topic = msg.topic;
      this.form1.groupId = msg.groupId;
      this.form1.tag = msg.tag;
      this.tableData = JSON.parse(msg.attrs);
      this.form.name = msg.name;
      (this.form.content = msg.content),
        (this.form.queueName = msg.queueName),
        (this.form.topic = msg.topic),
        (this.form.region = msg.region),
        (this.form.describes = msg.describes),
        (this.form.issueName = msg.issueName),
        (this.form.issueMail = msg.issueMail),
        (this.form.issueOrganization = msg.issueOrganization),
        (this.form.issueIphone = msg.issueIphone),
        (this.form.business = msg.business),
        (this.yw_lb_name = msg.business);
      // this.form
      this.$set(this.form, "id", msg.id);
      // this.form = msg
      this.isAlert = true;
    },
  },
};
</script>
<style lang="stylus" scoped>

.cascade >>> .el-input__inner::placeholder{
  color:#606266
}
.datacenter_con {
  margin: 16px;
  padding: 20px;
  position: relative;
  background: #fff;
}

.datacenter_con .tit {
  display: flex;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #dadff5;
}

.datacenter_con .tit .line {
  width: 2px;
  height: 13px;
  background: #4a7bff;
}

.datacenter_con .tit .text {
  color: #4a7bff;
  font-size: 14px;
  font-weight: 600;
  margin-left: 10px;
}

.btn_row {
  display: flex;
  justify-content: space-between;
  margin: 20px 0 20px;
}

.search_box {
  display: flex;
  align-items: center;
}

.search_box >>> .el-input {
  width: 280px;
}

.search_box >>> .el-input__inner {
  height: 32px;
  border-top-right-radius: 0px;
  border-bottom-right-radius: 0px;
}

.search_box >>> .el-button--small {
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-active {
  opacity: 0;
}

.detail .detail_box {
  width: 600px;
}

.detail .detail_box_form {
  width: 800px;
  height: 600px;
  overflow-y: auto;
}

.from_box_text {
  font-size: 20px;
}

.form_item {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.form_item >>> .el-input__inner, .form_item >>> .el-textarea__inner {
  width: 260px;
}

.btn_box {
  display: flex;
  justify-content: flex-end;
}

>>> .el-menu--horizontal>.el-submenu .el-submenu__title {
  width: 260px;
  border: 1px solid #DCDFE6;
  border-radius: 5px;
  height: 40px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  color: #C0C4CC;
}

.timeBox {
  width: 260px;
  display: flex;
  align-items: center;
}

.timeBox >>> .el-input__inner {
  width: 100%;
}

.timeBox >>> .el-date-editor.el-input {
  width: 100%;
}

.table_content {
  display: flex;
  position: relative;
  margin-bottom: 30px;
}

.table_content .table_label {
  width: 100px;
  text-align: right;
  font-size: 14px;
  color: #606266;
  padding-right: 10px;
  box-sizing: border-box;
}

.table_label {
  font-size: 20px;
  margin-left: -22px;
  margin-bottom: 18px;
}

.table_content >>> .el-table td, .table_content >>> .el-table th {
  padding: 0;
}

.table_content >>> .el-input {
  margin: 10px 0;
}

.table_content >>> .el-input__inner {
  font-size: 12px;
}

.table_tab {
  flex: 1;
}

.table_content .icon_box {
  position: absolute;
  width: 20px;
  height: 20px;
  text-align: center;
  lin-height: 20px;
  font-size: 22px;
  cursor: pointer;
  right: 0;
  top: -25px;
}

.delete_box {
  display: flex;
  justify-content: center;
  cursor: pointer;
}

#top_margin {
  margin-top: -40px;
}

#top_margin, .from_box >>> .el-form-item__label {
  font-size: 14px;
}

.edit_text_left {
  text-align: left;
}

.table_tab >>>.el-table th>.cell {
  font-size: 0.1rem;
}

.table_contentt {
  font-size: 0.1rem;
  margin-bottom: -0.128571rem;
}

.datacenter_con >>> .el-table__row .el-table_1_column_1 {
  width: 500px !important;
}

#yylb >>> .el-submenu__title {
  width: 1.871429rem;
  display: flex;
  justify-content: space-between;
}

#yylb >>> .el-menu--horizontal>.el-submenu .el-submenu__icon-arrow {
  margin-left: 175px;
  font-size: 14px;
  color: #ccc;
}



</style>
